<template>
    <div class="commodity">
        <Search @searchData="getCommodityData" @resetData="getCommodityData"></Search>
        <AddAndDel @delsByIds="delsByIds"></AddAndDel>
        <el-card>
            <el-table ref="multipleTable" class="custom-table" height="450px"
                :default-sort="{ prop: 'date', order: 'descending' }"
                :header-cell-style="{ background: '#f5f7fa', color: '#606266' }" :data="tableData" tooltip-effect="dark"
                style="width: 100%" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column prop="id" label="序号" width="80" sortable>
                    <template slot-scope="scope">{{ scope.row.id }}</template>
                </el-table-column>
                <el-table-column prop="productImg" label="商品图片" width="100">
                    <template slot-scope="scope">
                        <el-image class="productImg" :src="scope.row.productImg" :preview-src-list="srcList">
                        </el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="productName" label="商品名称">
                </el-table-column>
                <el-table-column prop="description" label="商品描述" width="250" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="price" label="商品价格">
                </el-table-column>
                <el-table-column prop="stock" label="商品库存">
                </el-table-column>
                <el-table-column prop="saleNum" label="商品销量">
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleEdit(scope.row)" plain>编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)" plain>删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <Pagination :paginationData="paginationData" @getPaginationData="getCommodityData"></Pagination>
        </el-card>
        <CommodityDialog @addCommodity="addCommodity" @getCommodityData="getCommodityData"></CommodityDialog>
    </div>
</template>
<script>
import Search from '../../components/search/index.vue'
import AddAndDel from '../../components/add_and_del/index.vue'
import Pagination from '../../components/pagination/index.vue'
import CommodityDialog from './dialog/index.vue'
export default {
    name: 'Commodity',
    components: {
        Search,
        AddAndDel,
        Pagination,
        CommodityDialog
    },
    mixins: [],
    props: {

    },
    data() {
        return {
            tableData: [],
            multipleSelection: [],
            paginationData: {},
            srcList: []
        }
    },
    computed: {

    },
    watch: {

    },
    mounted() {
        this.getCommodityData()
    },
    methods: {
        // 获取商品信息
        getCommodityData(obj = { currentPage: 1, pageSize: 10, keyword: '' }) {
            this.request.post('/product/selectProductPage', obj).then(res => {
                if (res.code == 20000) {
                    res.data.list.forEach(item => {
                        if (item.createdDt) {
                            item.createdDt = item.createdDt.slice(0, 10)
                        }
                    })
                    res.data.list.forEach(item => {
                        if (item.price) {
                            item.price = item.price.toFixed(2)
                        }
                    })
                    this.tableData = res.data.list
                    this.paginationData = res.data
                    this.srcList = res.data.list.map(item => item.img)
                }
            })
        },
        // 新增 商品
        addCommodity(obj) {
            this.request.post('/product/productAdit', obj).then(res => {
                if (res.code == 20000) {
                    // 重新获取商品列表
                    this.getCommodityData()

                }
            })
        },
        //批量删除
        handleSelectionChange(selectedData) {
            // 被选中的数据的id
            this.multipleSelection = selectedData.map(item => item.id);
            this.$bus.$emit('multipleSelection', this.multipleSelection)

        },
        // 批量删除
        delsByIds() {
            this.request.post('/product/productDelByIds', { ids: this.multipleSelection }).then(res => {
                if (res.code == 20000) {
                    this.getCommodityData()
                    this.$message({
                        type: 'success',
                        message: '删除成功~'
                    })
                }
            })
        },
        // 编辑商品信息
        handleEdit(row) {
            this.$bus.$emit('add', true)
            this.$bus.$emit('getForm', row)
        },
        // 删除商品信息
        handleDelete(row) {
            this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.request.post('/product/productDel', { id: row.id }).then(res => {
                    if (res.code == 20000) {
                        this.getCommodityData()
                        this.$message({
                            type: 'success',
                            message: '删除成功~'
                        })

                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        }
    }
};
</script>
<style lang='less' scoped>
.commodity {
    .el-table {
        font-size: 12px;
    }

    .productImg {
        width: 50px;
        height: 50px;
    }

    .el-pagination.is-background .el-pager li:not(.disabled).active {
        background-color: #11A983 !important;
        color: #FFF;
    }

}
</style>